package com.youyou.action;

import com.youyou.entity.StudentEntity;
import com.youyou.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class StudentAction {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/")
    private String getList(Model model){
       List<StudentEntity> studentEntityList= studentService.studentList();
       model.addAttribute("studentEntity",studentEntityList);
//        System.out.println(studentEntityList);
        return "index";
    }

    @RequestMapping("/toinsert")
    private String toinsert(Model model){
        return "InsertStudent";
    }

    @RequestMapping("/toinsertAction")
    private String toinsertAction(StudentEntity studentEntity){
        studentService.insertStudent(studentEntity);
        return "redirect:/";
    }

    @RequestMapping("/toUpdate")
    private String toUpdate(Model model,Integer id,StudentEntity studentEntity){
       studentEntity= studentService.getStudentById(id);
        model.addAttribute("stu",studentEntity);
        System.out.println(studentEntity.getName());
        return "UpdateStudent";
    }
    @RequestMapping("/toUpdateAction")
    private String toUpdateAction(StudentEntity studentEntity,Integer id){
       studentService.getStudentById(id);
        studentService.updateStudent(studentEntity);
        return "redirect:/";
    }
    @RequestMapping("/delActiom")
    private String delActiom(Model model, Integer id){
//        System.out.println(id);
        studentService.delStdentById(id);
        return "redirect:/";
    }
}
